package com.puyuan.web;

import com.puyuan.domain.PageBean;
import com.puyuan.domain.Student;
import com.puyuan.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/stu")
@MultipartConfig
public class StudentServlet extends BaseServlet {
    StudentServiceImpl studentService = new StudentServiceImpl();
    /**
     * 添加学员
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决乱码
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        //获取学生所有信息
        Map<String, String[]> map = req.getParameterMap();
        //把map中的数据封装到一个学生对象
        Student s = new Student();
        try {
            BeanUtils.populate(s,map);
            //爱好只会把第一个添加进去 需要处理
            String[] hobby = req.getParameterValues("hobby");
            s.setHobby(Arrays.toString(hobby));
            //处理头像：1. 获取文件名封装到对象 2.把头像上传到服务器的磁盘上
            Part part = req.getPart("fileName");
            String fileName = part.getSubmittedFileName();
            s.setPhoto(fileName);
            //定义头像上传的路径
            String path = "D:\\img\\" + UUID.randomUUID() + fileName;
            part.write(path);
            //交给service
            int row = studentService.addStu(s);
            if (row > 0){
                //添加成功  访问展示学员的请求进行学员的展示
                resp.sendRedirect("/day05/stu?method=showStu");
            }else {
                throw new RuntimeException("添加失败");
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 展示学员
     * @param req
     * @param resp
     */
    public void showStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //分页展示
        String cp = req.getParameter("currentPage");
        String ps = req.getParameter("pageSize");
        //查询条件
        String name1 = req.getParameter("name1");
        String gender1 = req.getParameter("gender1");
        //查询学员  响应回展示学院的jsp页面进行展示
        PageBean<Student> pageBean = studentService.findStu(cp,ps,name1,gender1);
        if (pageBean != null){
            //查到的学员信息 存到域对象
            req.setAttribute("pageBean",pageBean);
            //请求转发到展示学生的页面上
            req.getRequestDispatcher("/stu/showStu.jsp").forward(req,resp);
        }else {
            throw new RuntimeException("无法展示，请添加数据");
        }
    }

    /**
     * 删除学员
     * @param req
     * @param resp
     */
    public void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String ids = req.getParameter("ids");
        int row = studentService.deleteStu(ids);
        if (row > 0){
            //删除成功 重新展示学员信息
            resp.sendRedirect("/day05/stu?method=showStu");
        }else {
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 根据学生id查询对应的学员信息
     * @param req
     * @param resp
     */
    public void findStuById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        int id = Integer.parseInt(sid);
        Student s = studentService.findStuById(id);
        if (s != null){
            //把查询到的s存到域对象中
            req.setAttribute("s",s);
            //响应到修改学生信息的jsp
            req.getRequestDispatcher("/stu/modifyStu.jsp").forward(req,resp);
        }else {
            throw new RuntimeException("查询失败");
        }
    }

    /**
     * 修改学员
     * @param req
     * @param resp
     */
    public void modifyStu(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException, ServletException {
        //获取修改后的所有的学生信息
        Map<String, String[]> map = req.getParameterMap();
        Student s = new Student();
        BeanUtils.populate(s,map);
        String[] hobbies = req.getParameterValues("hobby");
        s.setHobby(Arrays.toString(hobbies));
        //头像信息获取不到 单独处理
        Part part = req.getPart("fileName");
        String fileName = part.getSubmittedFileName();
        //如果没修改头像 fileName是null 或者是 ""
        if (fileName == null){
            String oldPhoto = req.getParameter("oldPhoto");
            s.setPhoto(oldPhoto);
        }else {
            s.setPhoto(fileName);
            //上传
            String path = "D:\\img\\" + UUID.randomUUID() + fileName;
            part.write(path);
        }
        String sid = req.getParameter("sid");
        int id = Integer.parseInt(sid);
        s.setSid(id);
        //对象信息封装完毕  进行修改
        int row = studentService.modifyStu(s);
        if (row > 0){
            //修改成功  进行重新展示
            resp.sendRedirect("/day05/stu?method=showStu");
        }else {
            throw new RuntimeException("修改失败");
        }
    }
}
